package com.handinfo.android.core.resource;

import java.util.Enumeration;
import java.util.Hashtable;

public class Resource
{
	private Hashtable<ResHead, DWSerializable> m_res;

	public Resource()
	{
		m_res = new Hashtable<ResHead, DWSerializable>();
	}

	public void clean()
	{
		m_res.clear();
	}

	/**
	 * 获取key为head的对象
	 * @param head
	 * @return
	 */
	public DWSerializable getResource(ResHead head)
	{
		return m_res.get(head);
	}

	/**
	 * 以head为key将object put到m_res中
	 * 
	 * @param head
	 * @param object
	 */
	public void put(ResHead head, DWSerializable object)
	{
		object.m_resHead = head;
		m_res.put(head, object);
	}

	/**
	 * 移除key为head的对象
	 * 
	 * @param head
	 */
	public void remove(ResHead head)
	{
		DWSerializable obj = m_res.get(head);
		m_res.remove(head);
		if (obj != null) {
			obj.dispose();
		}
	}

	/**
	 * 移除m_res中所有对象
	 */
	public void removeAll()
	{
		Enumeration<DWSerializable> enumeration = m_res.elements();
		DWSerializable obj;
		while (enumeration.hasMoreElements()) {
			obj = enumeration.nextElement();
			if (obj != null) {
				obj.dispose();
			}
		}
		m_res.clear();
	}

	/**
	 * 返回m_res中是否有key为head 的对象
	 * 
	 * @param head
	 * @return
	 */
	public boolean exist(ResHead head)
	{
		return m_res.containsKey(head);
	}

	/**
	 * m_res是否还有Serializeable对象引用此head对应的对象
	 * 
	 * @param head
	 * @return
	 */
	public boolean isUseing(ResHead head)
	{
		Enumeration<DWSerializable> enumeration = m_res.elements();
		DWSerializable obj;
		while (enumeration.hasMoreElements()) {
			obj = enumeration.nextElement();
			if (obj != null) {
				if (obj.isContainRefHead(head)) {
					return true;
				}
			}
		}
		return false;
	}

	public ResourceList and(final ResourceList list)
	{
		ResourceList result = new ResourceList();
		ResHead head;
		list.reset();
		while (list.hasMoreElements()) {
			head = list.nextElement();
			if (exist(head)) {
				result.put(head);
			}
		}
		return result;
	}

	public ResourceList xor(final ResourceList list)
	{
		ResourceList result = new ResourceList();
		ResHead head;
		list.reset();
		while (list.hasMoreElements()) {
			head = list.nextElement();
			if (!exist(head)) {
				result.put(head);
			}
		}
		return result;
	}

	public ResourceList rxor(final ResourceList list)
	{
		ResourceList result = new ResourceList();
		Enumeration<ResHead> enumeration = m_res.keys();
		ResHead head;
		while (enumeration.hasMoreElements()) {
			head = (ResHead) enumeration.nextElement();
			if (!list.exist(head)) {
				result.put(head);
			}
		}
		return result;
	}

	/**
	 * 使用后加载的对象inst来更新m_res中的对象
	 * 
	 * @param head
	 * @param inst
	 */
	public void updateResource(ResHead head, DWSerializable inst)
	{
		Enumeration<DWSerializable> enumeration = m_res.elements();
		DWSerializable obj;
		while (enumeration.hasMoreElements()) {
			obj = enumeration.nextElement();
			obj.updateComponent(head, inst);
		}
	}

	public boolean isUseing(DWSerializable host, ResHead head)
	{
		Enumeration<DWSerializable> enumeration = m_res.elements();
		DWSerializable obj;
		while (enumeration.hasMoreElements()) {
			obj = (DWSerializable) enumeration.nextElement();
			if (obj != null && obj != host) {
				if (obj.isContainHeader(head)) {
					return true;
				}
			}
		}
		return false;
	}
}
